<?php

class UnactivatedUserManager extends TModule
{
	// -------------------- basic functions --------------------
	public function addUnactivatedUser($login, $password, $first_name, $last_name, $email, $registration)
	{
		$user = new UnactivatedUserRecord();
		$user->setData(	0, $login, $password, $first_name, $last_name, $email, $registration, md5($login.$password.$email));
		
		if($user->save())
		{
			$setting_manager = Prado::getApplication()->Modules['settings'];
			$conference_name_setting = $setting_manager->getSettingByName(SettingManager::$CONFERENCE_NAME);
			$conference_name = 
				$conference_name_setting->getValue() ? $conference_name_setting->getValue() : 'Conference';
			
			$title =	Prado::localize('Register email title {conference_name}',
										array('conference_name' => $conference_name ));
			$text =		Prado::localize('Register email text {conference_name}',
										array('conference_name' => $conference_name )).
						"\n".$user->getFullLink()."\n\n".
						Prado::localize('Register email information {conference_name}',
										array('conference_name' => $conference_name ));

			if(Sendmail::send_mail($user->getEmail(), $title, $text))
				return $user;
			else
				return null;
		}
		else
			return null;
	}
	
	public function deleteUnactivatedUser($id)
	{
		return UnactivatedUserRecord::finder()->deleteById($id);
	}
	
	public function deleteUnactivatedUserByLink($link)
	{
		return UnactivatedUserRecord::finder()->deleteByLink($link);
	}
	
	public function activateUnactivatedUser($link)
	{
		$unactivatedUser = $this->getUnactivatedUserByLink($link);
		if($unactivatedUser)
		{
			$manager = $this->Application->Modules['users'];
			$result  = $manager->addUser(	$unactivatedUser->getLogin(), 
											$unactivatedUser->getPassword(), 
											$unactivatedUser->getFirstName(),
										 	$unactivatedUser->getLastName(),
										 	$unactivatedUser->getEmail(),
										 	$unactivatedUser->getRegistration());
			if($result)
				$this->deleteUnactivatedUserByLink($link);
				
			return $result;
		}
		return false;
	}
	
	public function getUnactivatedUserById($id)
	{
		return UnactivatedUserRecord::finder()->findByPk($id);
	}
	
	public function getUnactivatedUserByLink($link)
	{
		return UnactivatedUserRecord::finder()->findByLink($link);
	}
	
	// -------------------- extended functions --------------------
	public function getUnactivatedUserList($revert = false, $offset = -1, $limit = -1)
	{
		$criteria = new TActiveRecordCriteria();
		
		if($revert)
			$criteria->OrdersBy['registration'] = 'desc';
			
		if($offset < 0 || $offset == null)
			$offset = 0;
		if($limit < 0 || $limit == null)
			$limit = $this->countUnactivatedUsers();
			
		$criteria->Offset = $offset;
		$criteria->Limit = $limit;
		
		return UnactivatedUserRecord::finder()->findAll($criteria);
	}
	
	public function countUnactivatedUsers()
	{
		return UnactivatedUserRecord::finder()->count();
	}
	
	public function isLoginReserved($login)
	{
		$reserved = UnactivatedUserRecord::finder()->findByLogin($login);

		if($reserved)
			return true;
		return false;
	}
}

?>